import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import { getToken } from './auth'
var interceptor = axios.create({
  baseURL: 'http://10.212.16.9:2859/mannger/', // 请求地址
  timeout: 5000 // 请求超时设置
})

// request interceptor
interceptor.interceptors.request.use(
  config => {
    // 配置头部请求
    config.headers.Authorization = getToken()
    return config
  },
  error => {
    // do something with request error
    return Promise.reject(error)
  }
)

// response interceptor
interceptor.interceptors.response.use(
  response => {
    const res = response.data
    // if the custom code is not 20000, it is judged as an error.
    if (res.status === 1) {
      Message({
        message: res.msg || 'Error',
        type: 'error',
        duration: 5 * 1000
      })
      return Promise.reject(new Error(res.msg || 'Error'))
    } else {
      return res
    }
  },
  error => {
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default interceptor
